

package com.hazelcast.cardinality;

import com.hazelcast.cardinality.impl.CardinalityEstimatorContainer;
import com.hazelcast.cardinality.impl.CardinalityEstimatorService;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.spi.impl.NodeEngineImpl;

import static com.hazelcast.test.Accessors.*;

final class CardinalityEstimatorTestUtil {

    private CardinalityEstimatorTestUtil() {
    }

    /**
     * Returns the backup instance of an {@link CardinalityEstimator} by a given cardinality estimator instance.
     * <p>
     * Note: Returns the backups from the first replica index.
     *
     * @param instances            the {@link HazelcastInstance} array to gather the data from
     * @param cardinalityEstimator the {@link CardinalityEstimator} to retrieve the backup from
     * @return the backup estimation
     */
    static long getBackupEstimate(HazelcastInstance[] instances, CardinalityEstimator cardinalityEstimator) {
        int partitionId = getPartitionIdViaReflection(cardinalityEstimator);
        HazelcastInstance backupInstance = getFirstBackupInstance(instances, partitionId);
        return getBackupEstimate(backupInstance, cardinalityEstimator.getName());
    }

    /**
     * Returns the backup estimation of an {@link CardinalityEstimator} by a given cardinality estimator name.
     * <p>
     * Note: You have to provide the {@link HazelcastInstance} you want to retrieve the backups from.
     * Use {@link getBackupInstance} to retrieve the backup instance for a given replica index.
     *
     * @param backupInstance the {@link HazelcastInstance} to retrieve the backup estimation from
     * @param estimatorName  the cardinality estimator name
     * @return the backup estimation
     */
    static long getBackupEstimate(HazelcastInstance backupInstance, String estimatorName) {
        NodeEngineImpl nodeEngine = getNodeEngineImpl(backupInstance);
        CardinalityEstimatorService service = nodeEngine.getService(CardinalityEstimatorService.SERVICE_NAME);
        CardinalityEstimatorContainer container = service.getCardinalityEstimatorContainer(estimatorName);
        return container.estimate();
    }
}
